package com.example.goodpet.my.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.goodpet.R;
import com.example.goodpet.activity.PetInfoActivity;
import com.example.goodpet.activity.TieziInfoActivity;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.User;
import com.example.goodpet.bean.UserTieziInfo;
import com.example.goodpet.db.DatabaseHelper;
import com.example.goodpet.my.fragment.MyPetInfoFragment;
import com.example.goodpet.my.fragment.MyTieziInfoFragment;
import com.example.goodpet.service.CommunityService;
import com.example.goodpet.service.PetInformationService;
import com.example.goodpet.utils.AppNetConfig;
import com.example.goodpet.utils.Constants;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MyReleaseTieziAdapter  extends RecyclerView.Adapter<MyReleaseTieziAdapter.ViewHolder>{
    private List<UserTieziInfo> lists;
    private Context context;
    private MyTieziInfoFragment myTieziInfoFragment;
    public MyReleaseTieziAdapter(List<UserTieziInfo> lists, Context context,MyTieziInfoFragment myTieziInfoFragment) {
        this.lists = lists;
        this.context=context;
        this.myTieziInfoFragment=myTieziInfoFragment;
    }

    @NonNull
    @Override
    public MyReleaseTieziAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_my_release, parent,false);
        return new MyReleaseTieziAdapter.ViewHolder(inflate);
    }



    @Override
    public void onBindViewHolder(@NonNull MyReleaseTieziAdapter.ViewHolder holder, int position) {

        UserTieziInfo userTieziInfo = lists.get(position);
        holder.tvPostdate.setText(userTieziInfo.getPostdate().toString());
        holder.tvContent.setText(userTieziInfo.getContent());
        Glide.with(context).load(Constants.BASE_URL_IMAGE+userTieziInfo.getTieziImage().get(0)).into(holder.petPhoto);

        initListener(holder,position);
    }


    private void initListener(@NonNull MyReleaseTieziAdapter.ViewHolder holder, int position) {

        holder.layoutEnter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TieziInfoActivity.class);
                UserTieziInfo userTieziInfo = lists.get(position);
                //Log.e(TAG, "onClick: ");
                intent.putExtra("userTieziInfo",MyApplication.ConvertUserTieziInfoForTransfer(userTieziInfo));
                intent.putExtra("position",position);
                myTieziInfoFragment.getActivity().startActivityForResult(intent,1);
            }
        });
        holder.tvDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(context)
                        .setTitle("温馨提示")
                        .setMessage("您确定要删除吗？")
                        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //连接服务器,传送宠物id
                                CommunityService communityService = RetrofitSingleton.getRetrofitNetConfig().create(CommunityService.class);
                                communityService.deleteByTieziId(lists.get(position).getTieziId()).enqueue(new Callback<ResponseBody<String>>() {
                                    @Override
                                    public void onResponse(Call<ResponseBody<String>> call, Response<ResponseBody<String>> response) {
                                        ResponseBody<String> body = response.body();
                                        System.out.println("MyReleaseAdapter.onResponse"+body);
                                        int code = response.body().getCode();
                                        if (code==200){
                                            DatabaseHelper.getInstance(UIUtils.getContext()).getTieziHistoryDAO().deleteByUserIdAndTieziId(MyApplication.user.getId(),lists.get(position).getTieziId());
                                            myTieziInfoFragment.notifyDataList(position);
                                            //lists.remove(position);
                                            //刷新
                                            //notifyItemRemoved(position);
                                            //notifyDataSetChanged();
                                        }else if(code==500){
                                            UIUtils.toast("服务器错误",false);
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody<String>> call, Throwable t) {
                                        UIUtils.toast("连接服务器失败",false);
                                    }
                                });

                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        })
                        .setCancelable(false).show();
            }
        });
    }


    @Override
    public int getItemCount() {
        return lists.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{
        TextView tvPostdate;
        TextView tvContent;
        ImageView petPhoto;
        TextView tvDelete;
        LinearLayout layoutEnter;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvPostdate=itemView.findViewById(R.id.tvPostdate);
            tvContent=itemView.findViewById(R.id.tvOverView);
            petPhoto=itemView.findViewById(R.id.petPhoto);
            tvDelete=itemView.findViewById(R.id.tvDelete);
            layoutEnter=itemView.findViewById(R.id.layoutEnter);


        }


    }
}
